package cn.hfatec.shms.web;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by Administrator on 2017-03-16.
 */
public class WeekTime {

    public  static String time()
    {
        Date date=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String time=format.format(date);
        return time;
    }
    public  static String timeBefore()
    {
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DAY_OF_MONTH,-3);
    String  dayBefore=  sf.format(c.getTime());
     return  dayBefore;
    }

    public  static String getBzstart() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
           String bzstart = df.format(cal.getTime());
        return bzstart;
    }

    public static  String getBzend(String bzStart) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date myDate = null;
        String bzend = null;
        try {
            myDate = formatter.parse(bzStart);
            Calendar c = Calendar.getInstance();
            c.setTime(myDate);
            c.add(Calendar.DATE, 6);
            myDate = c.getTime();
            bzend = formatter.format(myDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return bzend;
    }
    public static  String getDateTime(String bzStart,Integer i) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date myDate = null;
        String bzend = null;
        try {
            myDate = formatter.parse(bzStart);
            Calendar c = Calendar.getInstance();
            c.setTime(myDate);
            c.add(Calendar.DATE, i);
            myDate = c.getTime();
            bzend = formatter.format(myDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return bzend;
    }
    public  static String getMonthFirstDay() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        String monthStart = df.format(cal.getTime());
        cal.add(Calendar.MONTH, 1);//月增加1天
        cal.add(Calendar.DAY_OF_MONTH, -1);//日期倒数一日,既得到本月最后一天
        String end = df.format(cal.getTime());
        return  monthStart;
    }
    public  static String getMonthLastDay() {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        cal.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        cal.add(Calendar.MONTH, 1);//月增加1天
        cal.add(Calendar.DAY_OF_MONTH, -1);//日期倒数一日,既得到本月最后一天
        String monthEnd = df.format(cal.getTime());
        return  monthEnd;
    }
    public static String  getFirstDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);
        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        cal.add(Calendar.DATE, -7);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String weekStrat = df.format(cal.getTime());
        return weekStrat;
    }



    // 获取某年的第几周的结束日期
    public static String getLastDayOfWeek(int year, int week) {
        Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);
        cal.add(Calendar.DATE, -1);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String weekEnd = df.format(cal.getTime());
        return weekEnd;
    }

    public static String getWeekNum () {
    Calendar c = Calendar.getInstance();
    String weekNum =String.valueOf(c.get(Calendar.WEEK_OF_YEAR)) ;
    return weekNum;
    }

    // 获取某年的第几周和年
    public static String[] getWeekOfYear(String testDate) throws ParseException {
        String [] weekYear=null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cl = Calendar.getInstance();
        cl.setTime(sdf.parse(testDate));
        int week = cl.get(Calendar.WEEK_OF_YEAR);
        weekYear[0]=String.valueOf(week);
        System.out.println(week);
        cl.add(Calendar.DAY_OF_MONTH, -7);
        int year = cl.get(Calendar.YEAR);
        if (week < cl.get(Calendar.WEEK_OF_YEAR)) {
            year += 1;
        }
        weekYear[1]=String.valueOf(year);
        return weekYear;
    }


    public static String getBeforeFirstMonthdate(){
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String   beforeFirst= format.format(calendar.getTime());
        return beforeFirst;
    }
    public static String getBeforeLastMonthdate(){
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String   beforeLast=    sf.format(calendar.getTime());
        return  beforeLast;
    }
}
